package com.cqut.forum.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.User;
import com.cqut.forum.service.ControlService;
import com.cqut.forum.service.PictureGroupService;
import com.cqut.forum.service.PictureService;
import com.cqut.forum.util.PackTo;

/**
 * 图片处理action
 * @author yangxun
 */
@Controller
public class PictureAction {

	@Resource
	private PictureService pictureService;
	@Resource
	private PictureGroupService pictureGroupService;
	@Resource
	private ControlService controlService;
	/**
	 * 获取全部最新照片
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("/picture/last_all_picture")
	@ResponseBody
	public List<Map<String, Object>> lastPicture(HttpServletRequest request,int index,int size){
		List<Map<String, Object>> pictures = pictureService.getLastPicture(index, size);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		int len = pictures.size();
		//设置日期格式
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		for(int i = 0;i<len;i++){
			Map<String, Object> m = new HashMap<String, Object>();
			Date d = (Date) pictures.get(i).get("upload_time");
			//设置展示日期
			m.put("time", format.format(d));
			//重新设置日期
			d.setHours(0);
			d.setMinutes(0);
			d.setSeconds(0);
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			Map<String, Object> pp = pictures.get(i);
			Map<String, Object> mapp = new HashMap<String, Object>();
			mapp.put("pictureSrc",pp.get("picture_address"));
			mapp.put("userPhoto", pp.get("user_photo"));
			mapp.put("userName", pp.get("user_name"));
			mapp.put("replyTimes", pp.get("review_times"));
			list.add(mapp);
			for(int j = i+1;j<len;j++){
				if(d.before((Date)pictures.get(j).get("upload_time"))){
					Map<String, Object> p = pictures.get(j);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("pictureSrc",p.get("picture_address"));
					map.put("userPhoto", p.get("user_photo"));
					map.put("userName", p.get("user_name"));
					map.put("replyTimes", p.get("review_times"));
					list.add(map);
					i++;
				}
			}
			m.put("info", list);
			result.add(m);
		}
		return result;
	}
	
	/**
	 * 获取朋友的照片，默认按照上传时间排序
	 * @param request
	 * @param index
	 * @param size
	 * @return
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("/picture/friend_all_picture")
	@ResponseBody
	public List<Map<String, Object>> friendPicture(HttpServletRequest request,int index,int size,
			String groupName){
		User user = (User) request.getSession().getAttribute("user");
		List<Map<String, Object>> pictures = pictureService.getFriendPicture(user.getUserId(), index, size);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> m:pictures){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("publishTime", ((Date)m.get("upload_time")).toLocaleString());
			map.put("replyTime", m.get("review_times"));
			map.put("pictureSrc", m.get("picture_address"));
			map.put("userPhoto", m.get("user_photo"));
			map.put("userName", m.get("user_name"));
			//初步设置为固定值
			map.put("height", 200);
			result.add(map);
		}
		return result;
	}
	@SuppressWarnings("deprecation")
	@RequestMapping("/picture/get_by_groupName")
	@ResponseBody
	public List<Map<String, Object>> getByGroupName(int index,int size,String groupName){
		//List<Map<String, Object>> groups = pictureGroupService.getGroupsLikeName(groupName);
		List<Map<String, Object>> pictures = pictureService.getByLikeName(groupName,index,size);
		
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> m:pictures){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pictureId", m.get("picture_id"));
			map.put("publishTime", ((Date)m.get("upload_time")).toLocaleString());
			map.put("replyTime", m.get("review_times"));
			map.put("pictureSrc", m.get("picture_address"));
			map.put("userPhoto", m.get("user_photo"));
			map.put("userName", m.get("user_name"));
			//初步设置为固定值
			map.put("height", 200);
			result.add(map);
		}
		return result;
	}
	/**
	 * 获取一个相册中的全部图片，默认安装上传时间排序
	 * @return
	 */
	@RequestMapping("/picture/group_pictures")
	@ResponseBody
	public List<Map<String, Object>> getGroupPictures(int groupId,int index,int size,
			String option,boolean asc){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		String orderCase = "review_times".equals(option)?"review_times":"upload_time";
		List<Map<String, Object>> pictures = pictureService.getByGroupId(groupId, index*size, size,orderCase,asc);
		result = PackTo.packPictureToMap(pictures);
		return result;
	}
	
	/**
	 * 响应进入我的相册页面后默认加载全部的图片的请求，按照上传日期排序
	 * @param request
	 * @return
	 */
	@RequestMapping("/picture/my_all_picture")
	@ResponseBody
	public Map<String, Object> getAllPicture(HttpServletRequest request,int index,int size,
			String option,boolean asc){
		User user = (User)request.getSession().getAttribute("user");
		Map<String, Object> result = new HashMap<String, Object>();
		String orderCase = "review_times".equals(option)?"review_times":"upload_time";
		//String param = " order by "+(option.equals("upload_time")?"upload_time":"review_times")+" "+(asc?"asc":"desc");
		List<Map<String, Object>> pictures = pictureService.getMyAllPicture(user.getUserId(), index, size,orderCase,asc);
		
		if(pictures.size()==0){
			return result;
		}
		
		result.put("ss", request.getSession().getServletContext().getAttribute("ss"));
		result.put("mp",PackTo.packPictureToMap(pictures));
		return result;
	}
	
	/**
	 * 获取最热门的照片
	 * 此处本该规定获取三张照片，但是为了功能扩展，增加了动态指定张数
	 * @param index
	 * @param size
	 * @return
	 */
	@RequestMapping("/picture/hot_picture")
	@ResponseBody
	public List<Map<String, Object>> getHotPicture(int index,int size){
		//该数据已经封装成了前台的展示数据形式
		List<Map<String, Object>> result = pictureService.getHotPicture(index, size);
		return result;
	}
	
	/**
	 * 按照用户点击按钮条件获取相应图片信息
	 * @param index
	 * @param size
	 * @return
	 */
	/*@RequestMapping("/picture/get_by_case")
	@ResponseBody
	public List<Map<String, Object>> getByUploadTimeCase(HttpServletRequest request,int index,int size,boolean asc){
		User user = (User)request.getSession().getAttribute("user");
		List<Map<String, Object>> pictures = null;
		return pictures;
	}*/
	
	@RequestMapping("/picture/visit_picture")
	@ResponseBody
	public List<Map<String, Object>> visitPicture(HttpServletRequest request,int targetId,
			int index,int size,boolean isgroup,String option,boolean asc){
		int groupId,pictureIndex = 0;
		//isgroup=flase代表targetId为图片Id
		if(!isgroup){
			groupId = pictureGroupService.getGroupId(targetId);
			pictureIndex = pictureService.getPictureIndex(groupId,targetId);
		}
		else{
			groupId = targetId;
			index = 0;
		}
		
		//List<Map<String, Object>> result = getGroupPictures(groupId,index*size+pictureIndex,size);
		//String orderCase = "review_times".equals(option)?"review_times":"upload_time";
		String orderCase = option!=null?option:"upload_time";
		List<Map<String, Object>> pictures = pictureService.getByGroupId(groupId, index*size+pictureIndex, size,orderCase,asc);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		result = PackTo.packPictureToMap(pictures);
		
		return result;
	}
	
	@RequestMapping("/picture/delete_picture")
	@ResponseBody
	public String deletePicture(int pictureId){
		pictureService.deleteById(pictureId);
		return "true";
	}
	
	@RequestMapping("/control/picture_control_change")
	@ResponseBody
	public String pictureControl(HttpServletRequest request,Integer[] pictureIds,
			String[] groupNames){
		User user = (User)request.getSession().getAttribute("user");
		List<Integer> pictures = java.util.Arrays.asList(pictureIds);
		List<String> names = java.util.Arrays.asList(groupNames);

		controlService.pictureTypeHandle(user,pictures, names);
		return "true";
	}
/*	@SuppressWarnings("deprecation")
	private List<Map<String,Object>> packPictureToMap(List<Picture> pictures){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Picture p:pictures){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("image", p.getPictureAddress());
			m.put("publishTime", p.getUploadTime().toLocaleString());
			m.put("replyTime", p.getReviewTimes());
			m.put("visitTime", p.getVisitTimes());
			m.put("height", 200);
			result.add(m);
		}
		
		return result;
	}*/
}
